/**
 * Copyright 2011 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.api.adwords.starterkit.wrappers;

import com.google.api.adwords.starterkit.db.keys.PlatformKey;
import com.google.api.adwords.starterkit.db.structures.PlatformStructure;
import com.google.api.adwords.starterkit.db.tables.PlatformTargetTable;
import com.google.api.adwords.starterkit.settings.CampaignSettings;

import java.sql.Connection;
import java.util.HashSet;

/**
 * The Class PlatformTargetsWrapper manages the platform target synchronization
 * process with the starterkit DB.
 * 
 * @author rodrigo.f.vale@gmail.com (Rodrigo Vale)
 */
public class PlatformTargetWrapper {
  private CampaignSettings campaignSettings;
  private CampaignWrapper campaignWrapper;
  private PlatformTargetTable platformTargetTable;

  /**
   * Instantiates a new platform target wrapper.
   * 
   * @param campaignWrapper the campaign wrapper
   * @param campaignSettings the starter kit settings
   * @param conn the connection to DB
   */
  public PlatformTargetWrapper(final CampaignWrapper campaignWrapper,
      final CampaignSettings campaignSettings, Connection conn) {
    this.campaignSettings = campaignSettings;
    this.campaignWrapper = campaignWrapper;

    platformTargetTable = new PlatformTargetTable(conn);

    sync();
  }

  /**
   * Synchronize the current PlatformTarget with the PlatformTarget in 
   * StarterKit DB.
   */
  public void sync() {
    HashSet<String> platformTargets = campaignSettings.getPlatformTargetList();
    PlatformKey platformKey = new PlatformKey(campaignWrapper.getLocalId(), "");
    HashSet<PlatformStructure> platformAdWordsTargets =
        platformTargetTable.get(platformKey);
    for (String platformTarget : platformTargets) {
      if (!platformAdWordsTargets.contains(platformTarget)) {
        add(platformTarget);
      }
    }
    for (PlatformStructure platformTarget : platformAdWordsTargets) {
      if (!platformTargets.contains(platformTarget.getPlatform())) {
        platformKey = new PlatformKey(platformTarget.getLocalCampaignId(), 
            platformTarget.getPlatform());
        platformTargetTable.del(platformKey);
        campaignWrapper.setModifiedPlatform(true);
      }
    }
  }

  /**
   * Adds a new Platform Target to this wrapper.
   */
  public void add(String platformTarget) {
    PlatformStructure platformStructure = new PlatformStructure(
        campaignWrapper.getLocalId(), 
        campaignWrapper.getCampaignId(),
        platformTarget);
    platformTargetTable.save(platformStructure);
    campaignWrapper.setModifiedPlatform(true);
  }
}
